<?php

if (!function_exists('random')) {
    /**
     * 随机字符串
     *
     * @param $length
     * @param int $type
     * @param string $chars
     * @return string
     */
    function random($length, $type = 1, $chars = '0123456789abcdefghijklmnopqrstuvwxyz')
    {
        if ($type == 1) { // 随机数
            $hash = sprintf('%0' . $length . 'd', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            if ($type == 3) $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $max = strlen($chars) - 1;
            for ($i = 0; $i < $length; $i++) $hash .= $chars[mt_rand(0, $max)];
        }

        return $hash;
    }
}

if (!function_exists('genPsw')) {
    /**
     * 密码生成
     * @param $password
     * @param string $salt
     * @return string
     */
    function genPsw($password, $salt = '')
    {
        return md5(md5($password) . $salt);
    }
}